package com.meida.module.bus.provider.service.impl;

import com.google.common.collect.Lists;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaDelete;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.admin.client.entity.BaseUser;
import com.meida.module.admin.provider.service.BaseUserService;
import com.meida.module.bus.client.entity.BusMajor;
import com.meida.module.bus.client.entity.BusUserMajor;
import com.meida.module.bus.provider.mapper.BusMajorMapper;
import com.meida.module.bus.provider.service.BusMajorService;
import com.meida.module.bus.provider.service.BusUserMajorService;
import com.meida.module.bus.provider.utils.MyFlymeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 专业表接口实现类
 *
 * @author flyme
 * @date 2020-12-16
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusMajorServiceImpl extends BaseServiceImpl<BusMajorMapper, BusMajor> implements BusMajorService {
    @Autowired
    private BaseUserService baseUserService;
    @Autowired
    private BusUserMajorService busUserMajorService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusMajor major, EntityMap extra) {
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusMajor> cq, BusMajor major, EntityMap requestMap) {
        cq.orderByDesc("major.createTime");
        return ResultBody.ok();
    }

    @Override
    public void saveUserMajors(Long userId, String... majorIds) {
        if (userId == null || majorIds == null) {
            return;
        }
        BaseUser user = baseUserService.getUserById(userId);
        if (user == null) {
            return;
        }
        // 先清空,在添加
        removeUserMajors(userId);
        if (majorIds.length > 0) {
            for (String majorId : majorIds) {
                BusUserMajor busUserMajor = new BusUserMajor();
                busUserMajor.setUserId(userId);
                busUserMajor.setMajorId(Long.parseLong(majorId));
                busUserMajorService.save(busUserMajor);
            }
            List<String> majorIdList = Arrays.asList(majorIds);
            Collection<BusMajor> busMajorList = listByIds(majorIdList);
            List<String> majorCodesList = busMajorList.stream().map(BusMajor::getMajorCode).collect(Collectors.toList());
            String majorCodesStr = FlymeUtils.listToString(majorCodesList, ',');
            String redisKey = "userMajorCodes_" + userId;
            redisUtils.set(redisKey, majorCodesStr);
        }
    }

    @Override
    public void removeUserMajors(Long userId) {
        CriteriaDelete cd = new CriteriaDelete();
        cd.eq("userId", userId);
        busUserMajorService.remove(cd);

    }

    @Override
    public List<String> getUserMajors(Long userId) {
        List<String> majorCodesList = Lists.newArrayList();
        String redisKey = "userMajorCodes_" + userId;
        if (redisUtils.hasKey(redisKey)) {
            String majorCodesStr = redisUtils.getString(redisKey);
            majorCodesList = MyFlymeUtils.stringToList(majorCodesStr);
        } else {
            CriteriaQuery<BusUserMajor> cq = new CriteriaQuery(BusUserMajor.class);
            cq.select("majorCode");
            cq.eq("userId", userId);
            cq.createJoin(BusMajor.class).setMainField("majorId").setJoinField("majorId");
            List<EntityMap> entityMapList = busUserMajorService.selectEntityMap(cq);
            majorCodesList = entityMapList.stream().map(entityMap -> entityMap.get("majorCode").toString()).collect(Collectors.toList());
            String majorCodesStr = FlymeUtils.listToString(majorCodesList, ',');
            redisUtils.set(redisKey, majorCodesStr);
        }
        return majorCodesList;
    }
}
